package com.cy.cuiyang_admin.controller;

import com.cy.cuiyang_admin.file.FileUtils;
import com.cy.cuiyang_admin.poi.ExcelUtil;
import com.cy.cuiyang_admin.pojo.*;
import com.cy.cuiyang_admin.service.EmpService;
import com.cy.cuiyang_admin.util.JsonResult;
import com.cy.cuiyang_admin.util.RedisUtil;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {

    private static final Logger logger = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private EmpService empService;

    /** 下载路径 */
    @Value("${pecs.profile}")
    private String profile;

    /**
     * 查询oracle表数据
     * @return
     */
    @ApiOperation("班组运营指标得分")
    @GetMapping("/queryBzIndexStati")
    @ApiImplicitParams({
    })
    public JsonResult<List<BzIndexStatiDto>> queryBzIndexStati(
    ) {
        JsonResult<List<BzIndexStatiDto>> jsonResult = JsonResult.newInstance();
        try {
            List<BzIndexStatiDto> pageResult = empService.queryBzIndexStati();
            jsonResult.setData(pageResult);
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("获取数据成功！");
        } catch (Exception e) {
            logger.error("获取数据发生异常", e);
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("获取数据发生异常！");
        }
        return jsonResult;
    }

    /**
     * 查询oracle表数据
     * @return
     */
    @ApiOperation("班组运营指标排名")
    @GetMapping("/queryBzIndexStatiPm")
    @ApiImplicitParams({
    })
    public JsonResult<List<BzIndexStatiPmDto>> queryBzIndexStatiPm(
    ) {
        JsonResult<List<BzIndexStatiPmDto>> jsonResult = JsonResult.newInstance();
        try {
            List<BzIndexStatiPmDto> pageResult = empService.queryBzIndexStatiPm();
            jsonResult.setData(pageResult);
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("获取数据成功！");
        } catch (Exception e) {
            logger.error("获取数据发生异常", e);
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("获取数据发生异常！");
        }
        return jsonResult;
    }

    @RequestMapping("/insert")
    public JsonResult<?> insert(EmpEntity empEntity){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int insert = empService.insert(empEntity);
        if(insert == 1){
            //添加redis缓存
            redisUtil.set("123",empEntity);
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据添加成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据添加失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/deleteById")
    public JsonResult<?>  deleteById(int id){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int deleteById = empService.deleteById(id);
        if(deleteById >= 1){
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据删除成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据删除失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/updateById")
    public JsonResult<?>  updateById(EmpEntity empEntity){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int updateById = empService.updateById(empEntity);
        if(updateById >= 1){
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据更新成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据更新失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/select")
    public JsonResult<List<EmpEntity>> select(){
        JsonResult<List<EmpEntity>> jsonResult = JsonResult.newInstance();
        List<EmpEntity> empEntityList = empService.select();
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntityList);
        String a = redisUtil.get("123").toString();//测试Redis缓存
        return jsonResult;
    }

    @RequestMapping("/selectById")
    public JsonResult<EmpEntity> selectById(int id){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        EmpEntity empEntity = empService.selectById(id);
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntity);
        return jsonResult;
    }
    @RequestMapping("/queryAll")
    public JsonResult<List<EmpEntity>> queryAll(){
        JsonResult<List<EmpEntity>> jsonResult = JsonResult.newInstance();
        List<EmpEntity> empEntitys = empService.queryAll();
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntitys);
        return jsonResult;
    }

    /**
     * 半成品，不做参考
     * @return
     */
    @RequestMapping("/export")
    public JsonResult<EmpEntity> export(){
        JsonResult<EmpEntity> jsonResult = null;
        try {
            jsonResult = JsonResult.newInstance();
            ExcelUtil<EmpEntity> excelUtil2 = new ExcelUtil<>(EmpEntity.class);
            List<EmpEntity> list = empService.queryAll();
            for (EmpEntity emp : list) {
                if (emp.getAge() == 1){
                    emp.setSexs("男");
                }else{
                    emp.setSexs("女");
                }
            }
            String fileName= excelUtil2.exportExcel(list, "用户", "用户表", "D:\\");
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg(fileName + "导出成功！");
        } catch (Exception e) {
            logger.error("导出失败！");
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("导出失败！");
        }
        return jsonResult;
    }


    /**
     * 导出统计汇总表数据
     *根据模板poi导出
     * @return
     */
    @ApiOperation("导出统计汇总表数据")
    @GetMapping("/exportTjhzData")
    public JsonResult exportTjhzData(HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult = JsonResult.newInstance();
        try {
            String setFileName = String.valueOf(System.currentTimeMillis());
            String fileName = null;
            String filePath = null;

            List<BzIndexStatiDto> pageResult = empService.queryBzIndexStati();
            ExcelUtil<BzIndexStatiDto> excelUtil2 = new ExcelUtil<>(BzIndexStatiDto.class);
            fileName = setFileName+"-班组指标统计表";
            filePath = profile+fileName;
            excelUtil2.exportExcelBzdf(pageResult, profile, fileName,"bzdf");


            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            byte[] bytes = fileName.getBytes("UTF-8");
            fileName = new String(bytes,"ISO-8859-1");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + fileName+".xlsx");
            FileUtils.writeBytes(filePath+".xlsx", response.getOutputStream());

            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("导出数据成功！");
        } catch (Exception e) {
            logger.error("导出数据发生异常", e);
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("导出数据失败！");
        }
        return jsonResult;
    }



}
